﻿using System;
using System.Linq;
using Xrm;

namespace WebAppWalkthrough
{
	public partial class WebForm_CodeBehind : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

            if (!IsPostBack)
            {
                Listar();
            }
		}

        private void Listar()
        {
            ContactsGridView.DataSource = CRM.Dominio.Contato.Me.Listar();
            ContactsGridView.DataBind();
        }

        protected void ContactsGridView_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {

            switch (e.CommandName)
            { 
               case "Editar":
                    pnlEdit.Visible = true;
                    break;
            }
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            Contact ct = new Contact();
            ct.FirstName = this.txtNome.Text;
            ct.LastName = this.txtSobrenome.Text;
            ct.Address1_City = this.txtCidade.Text;

            CRM.Dominio.Contato.Me.Salvar(ct);
            this.Listar();
            pnlEdit.Visible = false;
        }
	}
}